<?php
namespace app\admin\controller;
use app\Base;
use think\facade\Db;
class Order extends Base{
	
	private $type=array();
	
	protected function initialize()
    {
		$cursor=Db::name('order_type')->order("typeid","asc")->cursor();
		
		foreach($cursor as $val)
		{
			$this->type[$val['en']]=$val['name'];
		}
	}
	//private $request;
	//控制台首页信息
    public function index()
    {
		if(!$this->request->post('pageSize') || !$this->request->post('page'))
		{
			return $this->kerror('参数错误');
		}
		
		//
		$where=[];
		if($this->request->post('username')){
			$where[]=['userid','in',"select userid from zm_member where username like '%".$this->request->post('username')."%'"];
		}
		
		if($this->request->post('type'))
		{
			$where[]=['type','=',$this->request->post('type')];
		}
		
		if($this->request->post('status')!=='')
		{
			$where[]=['status','=',$this->request->post('status')];
		}
		
		if($this->request->post('paystatus')!=='')
		{
			$where[]=['paystatus','=',$this->request->post('paystatus')];
		}
		
		$pages=array('list_rows'=>$this->request->post('pageSize'),'page'=>$this->request->post('page'));
		
		
		$list=Db::name('order')->where($where)->paginate($pages)->each(function($item,$key){
			$user=Db::name('member')->where("userid",$item['userid'])->field("username,phone")->find();
			if($user['username'])
			{
				$item['username']=$user['username'];
				$item['username_phone']=$user['phone'];
				$item['rq']=date("Y-m-d",$item['inputtime']);
			}
			
			$item['ordertype']=$this->type[$item['type']];
			
			return $item;
		})->toArray()['data'];
		
		$total=Db::name('order')->where($where)->count();		
		
		$this->setobj(array('total'=>$total,'dataList'=>$list,'orderType'=>$this->type));
		return $this->ok();
    }
	
	public function delorder()
	{
		if(!$this->request->post('orderid'))
		{
			return $this->kerror('参数不正确');
		}
		
		$order=Db::name('order')->where('orderid',$this->request->post('orderid'))->find();
		if($order['status']==1 || ($order['paystatus']==1 && $order['status']<2))
		{
			return $this->kerror('当前订单状态不允许删除');
		}
		
		Db::name('order')->where('orderid',$this->request->post('orderid'))->delete();
		Db::name('order_'.$order['type'])->where('orderid',$this->request->post('orderid'))->delete();
		return $this->ok('删除成功');
	}
	
	public function orderinfo()
	{
		if(!$this->request->post('orderid'))
		{
			return $this->kerror('参数不正确');
		}
		$order=Db::name('order')->where('orderid',$this->request->post('orderid'))->find();
		if(!$order['orderid'])
		{
			return $this->kerror('订单不存在');
		}
		
		$info=Db::name('order_'.$order['type'])->where('orderid',$order['orderid'])->find();
		if($info['orderid'])
		{
			$order=array_merge($info,$order);
		}
		
		$member=Db::name('member')->where("userid",$order['userid'])->find();
		$order['username']=$member['username'];
		$order['typename']=$this->type[$order['type']];
		$order['statusname']=$order['status']==1?'已办理':$order['status']?'办理失败':'未办理';
		$order['paystatusname']=$order['paystatus']?'已支付':'未支付';
		$this->setobj($order);
		return $this->ok();
	}
	 
	
}
